package com.zhangtai.modules.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dto.PromotionEntityDto;
import com.zhangtai.modules.entity.employee.PromotionEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-07-30 09:53:02
 */
@Mapper
public interface PromotionDao extends BaseMapper<PromotionEntity> {

    @Select(
            "<script>" +
                    "Select promotion.name,promotion.promotion_id promotionId,promotion.oid,promotion.code,promotion.before_unit_id beforeUnitId," +
                    "promotion.before_org_id beforeOrgId,promotion.before_job_id beforeJobId,promotion.promotion_date promotionDate," +
                    "promotion.promotion_type promotionType,promotion.after_unit_id afterUnitId,promotion.after_org_id afterOrgId," +
                    "promotion.after_job_name afterJobName,promotion.after_job_id afterJobId,promotion.is_delete isDelete," +
                    "promotion.create_time createTime,promotion.begin_expedition_date beginExpeditionDate,promotion.end_expedition_date endExpeditionDate," +
                    "promotion.expedition_days expeditionDays,promotion.expedition_status expeditionStatus,promotion.before_job_level beforeJobLevel, " +
                    "promotion.before_job_type beforeJobType,promotion.before_job_level_id beforeJobLevelId,promotion.after_job_type afterJobType," +
                    "promotion.after_job_level_id afterJobLevelId,promotion.salary_group_id salaryGroupId,promotion.monthly_salary monthlySalary," +
                    "promotion.performance,promotion.reason,promotion.file_url fileUrl,promotion.applier,promotion.applier_code applierCode," +
                    "promotion.status,promotion.promotion_kind promotionKind,promotion.is_distribute isDistribute,promotion.check_url checkUrl," +
                    "promotion.is_remind isRemind,promotion.is_self isSelf,promotion.unit_id orgId," +
                    "apply.org_id applyOrgId,apply.unit_id applyUnitId,apply.name applyName," +
                    "apply.job_id applyJobId,apply.job_level_id applyJobLevelId from tb_promotion promotion  " +
                    "left join tb_employee_info info  on info.oid = promotion.oid  " +
                    "LEFT JOIN tb_employee_info apply on apply.code = promotion.applier_code" +
                    "<where> promotion.is_delete = 0 " +
                        "<if test='promotionType!=null'>and promotion_type = #{promotionType} </if>" +
                        "<if test='userCode!=null'> and info.code = #{userCode} </if>" +
                        "<if test='promotionKind!=null'> and promotion_kind = #{promotionKind} </if>" +
                        "<if test='expeditionStatus!=null'> and promotion.expedition_status = #{expeditionStatus} </if>" +
                        "<if test='isDistribute!=null'> and promotion.expedition_status = #{isDistribute} </if>" +
                        "<if test='name!=null'> and info.name like '%${name}%' </if>" +
                        "<if test='isSelf!=null'> and promotion.is_self = #{isSelf} </if>" +
                        "<if test='orgIds!=null and orgIds.size>0'> and promotion.before_unit_id in " +
                            " <foreach item=\"orgId\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{orgId}</foreach>" +
                        "</if>"+
                        "<if test='orgIds==null or orgIds.size==0'> and ( promotion.before_unit_id = null or info.code = #{userCode}) " +

                        "</if>"+
                    "<if test='status!=null'> and promotion.status = #{status} </if>" +
                    "</where>" +
                    " order by promotion.create_time desc" +
            "</script>"
    )
     List <PromotionEntityDto>getPromotionList(@Param("promotionType")Integer promotionType, @Param("userCode")String userCode,
                                               @Param("name")String name, @Param("orgName")String orgName,
                                               @Param("unitname")String unitname, @Param("expeditionStatus")Integer expeditionStatus,
                                               @Param("isDistribute")Integer isDistribute, @Param("orgIds") List<Long> orgIds,
                                               @Param("promotionKind")Integer promotionKind, @Param(value ="isSelf")Integer isSelf,
                                               @Param("status") Integer status,
                                               Page<PromotionEntityDto> page);

    @Select("Select * from tb_promotion where is_delete = 0 " +
            "and is_self = 1 " +
            "and status = 2 " +
            "and promotion_type = 2 " +
            "and expedition_status = 0 " +
            "and is_regular_remind = 0 " +
            "and TIMESTAMPDIFF(DAY,end_expedition_date,#{time}) <=30   order by end_expedition_date desc")
    List<PromotionEntity>sendPromotionRegularNotice(@Param("time") String time);

}
